package com.atguigu.queue;

import java.util.Scanner;

/**
 * @author 程旭东
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
       //编写测试程序
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;

        while(loop){
            System.out.println("s(show) 显示队列");
            System.out.println("e(exit) 退出程序");
            System.out.println("a(add) 添加数据到队列");
            System.out.println("g(get) 从队列获取数据");
            System.out.println("h(head) 查看队列头的数据");
            key = sc.next().charAt(0);
            switch(key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数：");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int queue = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n",queue);
                    }catch(RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int queue = arrayQueue.headQueue();
                        System.out.printf("头数据是%d\n",queue);
                    }catch(RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    return;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

class ArrayQueue{
    //队列最大值
    private int maxSize;
    //队列上标
    private int rear;
    //队列下标
    private int front;
    //队列本列
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.rear = -1;
        this.front = -1;
        this.arr = new int[maxSize];
    }

    /**
     * 判断队列是否已满
     * @return
     */
    public boolean isFull(){
        return rear == maxSize-1;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty(){
        return front == rear;
    }

    /**
     * 向队列中添加数据
     * @param n
     * @return
     */
    public void addQueue(int n){
        if(isFull()){
            System.out.println("队列满了，无法添加数据");
            return ;
        }
        rear++;
        arr[rear] = n;
    }

    /**
     * 从队列中取出数据
     * @return
     */
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，无法取出数据");
        }
        front++;
        return arr[front];
    }

    /**
     * 展示队列的全部数据
     */
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空，无法展示数据");
            return ;
        }
        for(int i=0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }

    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("头数据为空");
        }
        return arr[front+1];
    }
}
